package com.lry.basic.jvm.asm.transform_class.demo6;

import aj.org.objectweb.asm.ClassVisitor;
import aj.org.objectweb.asm.MethodVisitor;

public class AddMethodVisitor extends ClassVisitor {

    private final int methodAccess;
    private final String methodName;
    private final String methodDesc;
    private final String methodSignature;
    private final String[] methodExceptions;

    private boolean isMethodPresent;


    public AddMethodVisitor(int api, ClassVisitor cv, int methodAccess,
                            String methodName, String methodDesc,
                            String methodSignature, String[] methodExceptions) {
        super(api,cv);
        this.methodAccess = methodAccess;
        this.methodDesc = methodDesc;
        this.methodName = methodName;
        this.methodSignature = methodSignature;
        this.methodExceptions = methodExceptions;
    }



    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] interfaces) {
        if(name.equals(methodName)&&desc.equals(methodDesc)){
            isMethodPresent = true;
        }
        return super.visitMethod(access,name, desc, sig, interfaces);
    }

    @Override
    public void visitEnd() {
        if(!isMethodPresent){
            MethodVisitor mv = cv.visitMethod(methodAccess,methodName,methodDesc,methodSignature,methodExceptions);
            if(null!=mv)
                mv.visitEnd();
        }
        super.visitEnd();
    }
}
